home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Sources / UPointerObject.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  7.9 KB  |  302 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UPointerObject.cp 
  3. // Copyright Â© 1984-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UPOINTEROBJECT__
  7. #include "UPointerObject.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UFAILURE__
  13. #include "UFailure.h"
  14. #endif
  15.  
  16. #ifndef __UCOREUTILITIES__
  17. #include "UCoreUtilities.h"
  18. #endif
  19.  
  20. #ifndef __UMEMORY__
  21. #include "UMemory.h"
  22. #endif
  23.  
  24. #ifndef __UOBJECT__
  25. #include "UObject.h"
  26. #endif
  27.  
  28. #ifndef __UNIVERSALSTARTUP__
  29. #include "UniversalStartup.h"
  30. #endif
  31.  
  32.  
  33. // OpenDoc
  34.  
  35. #ifndef _MEMMGR_
  36. #include "MemMgr.h"
  37. #endif
  38.  
  39.  
  40. // ANSI
  41.  
  42. #ifndef __STDIO__
  43. #include <stdio.h>
  44. #endif
  45.  
  46. #ifndef __STDLIB__
  47. #include <stdlib.h>
  48. #endif
  49.  
  50. //========================================================================================
  51. // Defines
  52. //========================================================================================
  53.  
  54. // The default is to use MacApp's object heap for non-TObject structures. 
  55. // To revert to using calloc/malloc for structures, compile with -d qMAGlobalNew=0. 
  56.  
  57. #ifndef qMAGlobalNew
  58. #define qMAGlobalNew    1
  59. #endif
  60.  
  61. //========================================================================================
  62. // Global variable definitions
  63. //========================================================================================
  64.  
  65. Boolean                    pAllocateObjectsFromPerm = TRUE;    // Used to track whether to
  66.                                                             // allocate objects from
  67.                                                             // permanent memory or not.
  68.  
  69. //========================================================================================
  70. // GLOBAL Procedures
  71. //========================================================================================
  72. #undef Inherited
  73.  
  74. inline Boolean IsPointer(void *pointer)
  75. {
  76.     return (pointer != NULL && (((long)pointer & 1) != 1));
  77. }
  78.  
  79. static Boolean VerboseIsPointer(void *pointer);
  80.  
  81. //----------------------------------------------------------------------------------------
  82. // MAOperatorNew: 
  83. //----------------------------------------------------------------------------------------
  84. #pragma segment Main
  85.  
  86. void* MAOperatorNew(size_t size)
  87. {
  88.     // The following block ensures that, if we are invoked before InitUMemory has
  89.     // had a chance to instantiate gObjectHeap (i.e. at static initialization time), we
  90.     // nevertheless instantiate a minimal ObjectHeap.  Note that it will be expanded
  91.     // later on in InitUMemory to reflect the memory characteristics required by the app.
  92.     
  93.     if (gObjectHeap == NULL)
  94.     {
  95.         gObjectHeap = MMNewHeap( kMMAppMemory, (size_t)gSizeObjectHeap, (size_t)gSizeHeapIncrement,
  96.                 "Default MacApp Heap" );
  97.  
  98.         // Set the default heap if none has been set yet
  99.         if (!MMGetDefaultHeap())
  100.             MMSetDefaultHeap(gObjectHeap);
  101.     }
  102.     
  103.     void* blk = MMAllocateIn( size, gObjectHeap );
  104.     FailNIL(blk);
  105.  
  106.     return blk;
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // MAOperatorDelete: 
  111. //----------------------------------------------------------------------------------------
  112. #pragma segment Main
  113.  
  114. void MAOperatorDelete(void* obj)
  115. {
  116. #if qDebug
  117.     if (!gObjectHeap)
  118.         ProgramBreak("operator delete invoked prior to gObjectHeap being created!");
  119.     else
  120. #endif
  121.         MMFree(obj);
  122. }
  123.  
  124. #if qMAGlobalNew
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // operator new: 
  128. //----------------------------------------------------------------------------------------
  129. #pragma segment Main
  130.  
  131. void* operator new(size_t size)
  132. {
  133.    return MAOperatorNew(size);
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // operator delete: 
  138. //----------------------------------------------------------------------------------------
  139. #pragma segment Main
  140.  
  141. void operator delete(void* obj)
  142. {
  143.     MAOperatorDelete(obj);
  144. }
  145.  
  146. #endif // qMAGlobalNew
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // GetPermObjectAllocationState: 
  150. //----------------------------------------------------------------------------------------
  151. #pragma segment MAObjectRes
  152.  
  153. Boolean GetPermObjectAllocationState()
  154. {
  155.     return pAllocateObjectsFromPerm;
  156. } // GetPermObjectAllocationState 
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // AllocateObjectsFromPerm: 
  160. //----------------------------------------------------------------------------------------
  161. #pragma segment MAObjectRes
  162.  
  163. Boolean AllocateObjectsFromPerm(Boolean allocateFromPerm)
  164. {
  165.     Boolean previousState = pAllocateObjectsFromPerm;
  166.     pAllocateObjectsFromPerm = allocateFromPerm;
  167.     return previousState;
  168. } // AllocateObjectsFromPerm 
  169.  
  170. //----------------------------------------------------------------------------------------
  171. // FailNonObject: 
  172. //----------------------------------------------------------------------------------------
  173. #pragma segment MAObjectRes
  174.  
  175. void FailNonObject(TObject* obj)
  176. {
  177.     if (!IsObject(obj))
  178.     {
  179. #if qDebugMsg
  180.         char msg[80];
  181.         
  182.         VerboseIsObject(obj);                            // show why
  183.         sprintf(msg, "Object that failed discipline %p", obj);
  184.         ProgramBreak(msg);
  185. #endif
  186.         Failure(minErr, 0);
  187.     }
  188. } // FailNonObject 
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // FreeIfObject: 
  192. //----------------------------------------------------------------------------------------
  193. #pragma segment MAObjectRes
  194.  
  195. TObject* FreeIfObject(TObject* obj)
  196. {
  197.     if (obj)
  198.     {
  199. #if qDebug
  200.         if (!VerboseIsObject(obj))
  201.             ProgramBreak("In FreeIfObject: Not handed a valid object.");
  202.         else
  203.             // only free obj if it passes VerboseIsObject…
  204. #endif
  205.  
  206.         obj->Free();
  207.     }
  208.     return NULL;
  209. } // FreeIfObject 
  210.  
  211. //----------------------------------------------------------------------------------------
  212. // InitUObject: 
  213. //----------------------------------------------------------------------------------------
  214. #pragma segment MAInit
  215.  
  216. void InitUObject()
  217. {
  218.     ClassDesc::InitUClassDesc();
  219. } // InitUObject 
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // VerboseIsPointer: 
  223. //----------------------------------------------------------------------------------------
  224. #pragma segment MAObjectRes
  225.  
  226. Boolean VerboseIsPointer(void *pointer)
  227. {
  228.     // we should try to find a way to ask the memory manager if this is a valid pointer
  229.     return IsPointer(pointer);
  230. } // VerboseIsPointer 
  231.  
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // IsObject: 
  235. //----------------------------------------------------------------------------------------
  236. #pragma segment MAObjectRes
  237.  
  238. Boolean IsObject(TObject *obj)
  239. {
  240.     // Test objecthood 
  241.     
  242.     if ( !IsPointer(obj) )
  243.         return FALSE;
  244.         
  245. #if qDebug
  246.     if (gObjectHeap != NULL)
  247.     {
  248.         // check for valid heap block
  249. //SRF -temporary        if (!gObjectHeap->IsValidBlock(obj))
  250. //        if (!gObjectHeap->IsValidBlock(obj))
  251. //            return FALSE;
  252.         
  253.         // check for valid class ID
  254.         if (!ClassDesc::IsValidClassID(obj->fClassID))
  255.             return FALSE;
  256.     }
  257. #endif
  258.     
  259.     return TRUE;
  260. } // IsObject 
  261.  
  262.     
  263. //----------------------------------------------------------------------------------------
  264. // VerboseIsObject: 
  265. //----------------------------------------------------------------------------------------
  266. #pragma segment MADebug
  267.  
  268. Boolean VerboseIsObject(TObject *obj)
  269. {
  270.     if (VerboseIsPointer(obj))
  271.     {
  272. #if qDebug
  273.         if (!ClassDesc::IsValidClassID(obj->fClassID))
  274.             fprintf(stderr, "  That pointer is not a subclass of TObject.\n");
  275.         else
  276.         {
  277.             if (gObjectHeap != NULL)
  278.             {
  279.                 // check for valid heap block
  280. //SRF -temporary                if (!gObjectHeap->IsValidBlock(obj))
  281. //                {
  282. //                    fprintf(stderr, "  That pointer is not a valid heap block.\n");
  283. //                }
  284. //                else
  285.                     return TRUE;
  286.             }
  287.             else
  288.                 return TRUE;
  289.         }
  290. #else
  291.         return TRUE;
  292. #endif
  293.     }
  294.     
  295.     return FALSE;
  296. } // VerboseIsObject 
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // End of UPointerObject.cp
  300.  
  301. #pragma segment Inline
  302.